package cn.itcast.crm.service.impl;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
    //注入dao层
    @Autowired
    private CustomerRepository customerRepository;

    //未关联的客户
    @Override
    public List<Customer> findNoAssociationCustomers() {
        //查询null的客户的休眠时间
       /* try {
            Thread.currentThread().sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        return customerRepository.findByFixedAreaIdIsNull();
    }

    //查询指定某个区来关联的客户
    @Override
    public List<Customer> findHasAssociationFixedAreaCustomers(String fixedAreaId) {
        return customerRepository.findByFixedAreaId(fixedAreaId);
    }

    //ids  客户列表id
    //fid  定区id
    //改变关联的状态
    @Override
    public void associationCustomersToFixedArea(List<Integer> ids, String fid) {
        //每次在关联之前，先要清除定区下面的客户
        // customerRepository.clearAssociated(fid);
        System.out.println(fid);
        System.out.println(ids);

        customerRepository.updateFixedAreaId(fid, ids);

    }

    //客户注册
    @Override
    public Customer register(Customer customer) {
        //调用dao层
       return customerRepository.save(customer);
    }
    //根据电话查询客户
    @Override
    public Customer findByTelephone(String telephone) {
       return customerRepository.findByTelephone(telephone);
    }
    //激活客户
    @Override
    public void updatetype(Integer id) {
        customerRepository.updatetype(id);
    }

}



